﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstituteManager.Data;
using System.Data;

namespace InstituteManager.Bussiness
{
    public static class StaffBL
    {
        public static string errorMsg = "";
        public static List<tbl_Staff> getByAll()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_Staff> exhi = InsMana.tbl_Staff.ToList();
            return exhi;
        }

        public static tbl_Staff getById(int Id)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Staff.First(temp => temp.StaffId == Id);
        }

        public static tbl_Staff getByUsername(string Username)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Staff.First(temp => temp.Username == Username);
        }

        public static bool AddNew(tbl_Staff ex)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                // command to cach exception username exists
                try
                {
                    InsMana.tbl_Staff.First(temp => temp.Username == ex.Username);
                    errorMsg = "Username is exists ! ";
                    return false;
                }
                catch (Exception)
                {
                    InsMana.AddTotbl_Staff(ex);
                }

                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Update(tbl_Staff ex)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Staff obj = InsMana.tbl_Staff.First(temp => temp.StaffId == ex.StaffId);
                obj.StaffName = ex.StaffName;
                obj.Address = ex.Address;
                obj.DOB = ex.DOB;
                obj.Phone = ex.Phone;
                obj.Email = ex.Email;
                obj.Permission = ex.Permission;
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Delete(int Id, string currentUsername, int Permission)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Staff obj = InsMana.tbl_Staff.First(temp => temp.StaffId == Id);
                if (currentUsername == obj.Username)
                {
                    errorMsg = "You can't to delete yourself !";
                    return false;
                }
                if (Permission > obj.Permission)
                {
                    errorMsg = "You don't permission to delete this user !";
                    return false;
                }
                InsMana.DeleteObject(obj);
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool checkLogin(string user, string pass, ref int permission)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                permission = InsMana.tbl_Staff.First(temp => temp.Username == user && temp.Password == pass).Permission;
            }
            catch (Exception)
            {
                permission = 0;
                return false;
            }            
            return true;
        }

        public static List<tbl_Staff> SearchByName(string StaffName)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();

            return (from sc in InsMana.tbl_Staff where sc.StaffName.Contains(StaffName) select sc).ToList<tbl_Staff>();            
        }

        public static List<tbl_Staff> getByPermission(int PermissionId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return (from sc in InsMana.tbl_Staff where sc.Permission == PermissionId select sc).ToList();                 
        }

        public static bool changePassword(string Username, string NewPassword)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                (from sc in InsMana.tbl_Staff where sc.Username == Username select sc).First().Password = NewPassword;
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            return true;
        }
   
    }
}
